package com.wuchao.controller;

import com.wuchao.service.serviceImpl.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author wuchao66
 * @Date 2022/7/5 14:19
 * @Version 1.0
 */
@WebServlet(name = "ServletController",urlPatterns = "/ServletController")
//注解 标注解释说明
public class ServletController extends HttpServlet {//中转页的协议
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流

        out.flush();
        out.close();
    }
//默认get
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
       // HttpSession session = (HttpSession)request.getSession();
        PrintWriter out = response.getWriter();//文件流
        String op = request.getParameter("op");
        if(op!=null){

            ServiceImpl service = new ServiceImpl();
            if(op.equals("delete")){//判断删除标志
                int id= Integer.parseInt(request.getParameter("id")) ;
                int update = service.update("delete from user where id=?", id);
                if(update>0){//回去判断
                    request.getRequestDispatcher("/demo3/main.jsp?isdelete1=t").forward(request,response);
                }else{
                    response.sendRedirect(request.getContextPath()+"/demo3/main.jsp?isdelete2=t");
                }

            }else   if(op.equals("add")){//判断删除标志
                String name = request.getParameter("name");
                String sex = request.getParameter("sex");

                int update = service.update("insert user(name,sex) values(?,?)", name,sex);
                if(update>0){//回去判断
                    request.getRequestDispatcher("main.jsp?isadd1=t").forward(request,response);
                }else{
                    response.sendRedirect("main.jsp?isadd2=t");
                }

            }else {
                response.sendRedirect("main.jsp?feifa=t");
            }

        }else{//空的回去
            response.sendRedirect("main.jsp?feifa=t");
        }
        out.flush();
        out.close();

    }
}
